package cs342.uwosh.edu.ContactsApp;

import cs342.uwosh.edu.ContactsApp.R;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;

public class EditContact extends Activity {
    /** Called when the activity is first created. */
	
	static long id;
	private Contact contact;
	private ContactsDataSource db;
	private EditText fNameEdit, lNameEdit, cPhoneEdit, hPhoneEdit, aPhoneEdit, addressEdit, emailEdit, commentEdit;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
    	switch(ContactsActivity.theme)
    	{
    		case 0:
    			this.setTheme(R.style.myDark);
    			break;
    		case 1:
    			this.setTheme(R.style.myLight);
    			break;
    		case 2:
    			this.setTheme(R.style.myMatrix);
    			break;
    	}
        super.onCreate(savedInstanceState);
        setContentView(R.layout.edit);
        
        fNameEdit = (EditText) findViewById(R.id.firstNameEditText);
    	lNameEdit = (EditText) findViewById(R.id.lastNameEditText);
    	cPhoneEdit = (EditText) findViewById(R.id.cellPhoneEditText);
    	hPhoneEdit = (EditText) findViewById(R.id.homePhoneEditText);
    	aPhoneEdit = (EditText) findViewById(R.id.altPhoneEditText);
    	addressEdit = (EditText) findViewById(R.id.addressEditText);
    	emailEdit = (EditText) findViewById(R.id.emailEditText);
    	commentEdit = (EditText) findViewById(R.id.commentEditText);
    	
    	db = new ContactsDataSource(this);
    	db.open();
    	contact = db.getContact(id);
    	db.close();
    	
    	String fName="";
    	String lName="";
    	int split = contact.getName().indexOf(' ');
    	if(split > 0) {
    		fName = contact.getName().substring(0, split);
    		lName = contact.getName().substring(split+1);
    	}else {
    		fName = contact.getName();
    	}
    	
    	fNameEdit.setText(fName);
    	lNameEdit.setText(lName);
    	cPhoneEdit.setText(contact.getCellPhoneNumber());
    	hPhoneEdit.setText(contact.getHomePhoneNumber());
    	aPhoneEdit.setText(contact.getAlternatePhoneNumber());
    	addressEdit.setText(contact.getAddress());
    	emailEdit.setText(contact.getEmail());
    	commentEdit.setText(contact.getComments());
    }
    
    public void submit(View view) {
    	Contact newContact = new Contact(EditContact.id,fNameEdit.getText() + " " + lNameEdit.getText(), addressEdit.getText().toString(), hPhoneEdit.getText().toString(), cPhoneEdit.getText().toString(), aPhoneEdit.getText().toString(), emailEdit.getText().toString(), commentEdit.getText().toString());
    	//System.out.println(contact);
    	db.open();
    	db.editContact(newContact);
    	db.close();
    	
    	
    	Intent i = new Intent(this, ContactsActivity.class);
    	startActivity(i);
    }
    
}